<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>添加权限</title>
	<#include "./comm/meta-base.ftl" />
	<#include "./comm/meta-easyui.ftl" />
	<#include "./comm/meta-comm.ftl" />
	<link href="${basePath}/static/css/privilege.css" rel="stylesheet" />
</head>
<body>

 	<div class="easyui-layout" fit="true" >
        <div data-options="region:'west'" style="width:300px;">
        
        	<form id="data_form" class="page-form"  method="post"  >
	   			<input type="hidden" name="paramsData" id="paramsData" />
				<div class="row" style="padding:10px 20px 0px 20px;">
					<label class=" fm-label">权限组名</label>
					<div class="fm-control">
						<input type="text" name="name" class="form-control easyui-validatebox" data-options="required:true"  />
					</div>
				</div>
			</form>
        
        </div>
        <div data-options="region:'center'">
        	 <table id="data_table" class="easyui-treegrid"  border="0"  rownumbers="true" fitColumns="true" fit="true"
	               data-options="
	                   url: '${basePath}/privilege/privilege-full-tree-json',
	                   method: 'get',
	                   rownumbers: true,
	                   idField: 'id',
	                   lines: true,
	                   singleSelect: true,
	                   toolbar:'#datagrid_toolbar',
	                   treeField: 'text'
	               ">
	           <thead>
	           <tr>
	                <th data-options="field:'text',width:200,formatter:treeFormatter">权限树</th>
	                <!--
	                <th data-options="field:'operateJSON',width:width(0.5),formatter:operatePriFormatter">操作权限</th>
	                -->
	            </tr>
	           </thead>
	       </table>
        </div>
    </div>
    

   <div class="page-form">
   <div class="buts-row-bottom row ">
		<a href="javascript:submitForm();"  class="btn btn-success">
				<i class="fa fa-check"></i>提 交</a>
		<a href="javascript:closeWindow('新增权限组');" class="btn btn-default">
				<i class="fa fa-times"></i>取 消</a>
	</div>
	</div>
			
</body>
</html>
<style>
.datagrid-row-selected{background:#fff;color:#000;}
.datagrid-cell-c1-operateNodes{overflow:visible !important;} 
.datagrid-row-over{background:#fff;}
</style>
<script type="text/javascript">
//提交表单
function submitForm(){
	var $pris=$(".privilege_class:checked");
	var priArray = new Array();
	for(var i=0;i<$pris.length;i++){
		var $pri=$($pris[i]);
		var priId=$pri.val();
		var $opts=$pri.parent().parent().parent().next("[field='operateJSON']").find(".ochecked");
		
		//拼装数据
		var optArray = new Array();
		for(var j=0;j<$opts.length;j++){
			var $opt=$($opts[j]);
			var operateId=$opt.attr("value")
			
			var optObj= {'operateId':operateId};
			optArray[j]=optObj;    
		}
		var priObj = {'privilegeId':priId,'operateList':optArray};
		priArray[i]=priObj;
	}
	var paramsData= JSON.stringify(priArray); 
	$("#paramsData").attr("value",paramsData);

	$("#data_form").form('submit', {
		url : '${basePath}/privilege-group/privilege-group-save',
		onSubmit: function (){
			showPageShade();//当提交时候，显示遮罩层
			if(!$(this).form('validate')){//页面验证
				hiddenPageShade();//未成功，隐藏遮罩层
				return false;
			}
			return true;
        },
		success : function(result) {
			hiddenPageShade();//成功后，隐藏遮罩层
			closeWindowRefrresh("新增权限组");
		}
	});
}


function operatePriFormatter(value,row,index){
	var html="";
	if(''!=value&&null!=value){
		var operateJSON = eval("(" + value + ")"); 
		for(var i=0;i<operateJSON.length;i++){
			var operateObj=operateJSON[i];
			if(operateObj.type=='Y10'){
				html+="<div value='"+operateObj.operateId+"' onclick='checkOpt(this)' class='operateDiv  operate-ck hidden ochecked' >"+operateObj.name+"</div>";
			}else{
				html+="<div value='"+operateObj.operateId+"' onclick='checkOpt(this)' class='operateDiv  operate-ck hidden ' >"+operateObj.name+"</div>";
			}
		}
	}
	return html;
}
function checkOpt(obj){
	var $this=$(obj);
	if($this.hasClass("ochecked")){
		$this.removeClass("ochecked");
	}else{
		$this.addClass("ochecked");
	}
	
}

function treeFormatter(value,row,index){
	var priClass="";
	if(row.type=='PRIVILEGE'){
		priClass='privilege_class';
	}
	var html="<input type='checkbox' onclick=\"checkTree('"+row.id+"')\"  class='tree-checkbox "+priClass+"' value='"+row.id+"' id='cid_"+row.id+"'  /><div class='tree-checkbox-text'>"+value+"</div>";
	return html;

}

function checkTree(id){
	var url="${basePath}/privilege/privilege-children/"+id;
	$.ajax({
		url: url,
		type: "POST",
		dataType:'json',
		success: function (data) {
			var ck= $("#cid_"+id).prop("checked");
			for(var i=0;i<data.length;i++){
				var domid="cid_"+data[i].privilegeId;
				if(ck){
					$("#"+domid).prop("checked",true);
					$("#"+domid).parent().parent().parent().next("[field='operateJSON']").find(".operateDiv").removeClass("hidden");
					
				}else{
					$("#"+domid).prop("checked",false);
					$("#"+domid).parent().parent().parent().next("[field='operateJSON']").find(".operateDiv").addClass("hidden");
				}
			}
			
		}
	});
}



</script>


